structure Main = 
struct

(* These are MAGIC! I just COPY from ml-yacc manual *)
structure CLrVals = CLrValsFun(structure Token = LrParser.Token)

structure CLex = CLexFun(structure Tokens = CLrVals.Tokens)

structure CParser = Join( structure LrParser = LrParser
                          structure ParserData = CLrVals.ParserData
			  structure Lex = CLex)


fun parse_stream stream = 
    let val lexer = CParser.makeLexer 
                        (fn i => TextIO.inputN(stream,i))
        val (ast,_) = CParser.parse(0,lexer,fn (_,_,_) => print "error\n",())
    in
        ast
    end

fun parse_file f = 
    let val s = TextIO.openIn f
        val r = parse_stream s
    in
        (r before TextIO.closeIn s;print ("Success!\n"))
    end
    handle _ => print ("Fail!\n")

fun parse_string s = (parse_stream (TextIO.openString s))

fun main () =
    let fun usage () = print "Lab3:Parser for C. \nUsage:lab3 filename"
        val fileName = CommandLine.arguments ()
    in  case fileName 
         of [] => usage ()
          | [f] => parse_file f
          | _ => 
            (print "too many files\n"
           ; usage ())
    end

end 
